package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.mapper.Sms_Sku_Full_Reduction_Mapper;
import com.shop.service.Sms_Member_Price_Service;
import com.shop.service.Sms_Sku_Full_Reduction_Service;
import com.shop.service.Sms_Sku_Ladder_Service;
import com.shop.pojo.sms.Sms_Member_Price;
import com.shop.pojo.sms.Sms_Sku_Full_Reduction;
import com.shop.pojo.sms.Sms_Sku_Ladder;
import com.shop.to.SkuByReductionTo;
import com.shop.utils.LayuiUtils;
import com.shop.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

//商品满减优惠逻辑层
@Service
public class Sms_Sku_Full_Reduction_ServiceImpl extends ServiceImpl<Sms_Sku_Full_Reduction_Mapper, Sms_Sku_Full_Reduction> implements Sms_Sku_Full_Reduction_Service {

  @Autowired
    private Sms_Sku_Ladder_Service sms_sku_ladder_service;//商品阶梯价格
  @Autowired
  private Sms_Member_Price_Service sms_member_price_service;//商品会员价钱

  @Autowired
  private Sms_Sku_Full_Reduction_Mapper sms_sku_full_reduction_mapper;

  @Transactional
    @Override
    public void saveReductionBySku(SkuByReductionTo skuByReductionTo) {
    //sku的优惠、满减等信息 sms_sku_ladder/sms_sku_full_reduction/sms_merber_price
      //1/sms_sku_ladder
      Sms_Sku_Ladder sms_sku_ladder=new Sms_Sku_Ladder();
      sms_sku_ladder.setSku_id(skuByReductionTo.getSku_id());
      sms_sku_ladder.setFull_count(skuByReductionTo.getFullCount());
      sms_sku_ladder.setDiscount(skuByReductionTo.getDiscount());
      sms_sku_ladder.setAdd_other(skuByReductionTo.getCountStatus());
      sms_sku_ladder_service.save(sms_sku_ladder);
      //2/sms_sku_full_reduction
      Sms_Sku_Full_Reduction sms_sku_full_reduction=new Sms_Sku_Full_Reduction();
      sms_sku_full_reduction.setSku_id(skuByReductionTo.getSku_id());
      sms_sku_full_reduction.setFull_price(skuByReductionTo.getFullPrice());
      sms_sku_full_reduction.setAdd_other(skuByReductionTo.getCountStatus());
      sms_sku_full_reduction.setReduce_price(skuByReductionTo.getReducePrice());
      this.save(sms_sku_full_reduction);
      //3/sms_merber_price
      List<Sms_Member_Price> sms_member_priceList = skuByReductionTo.getMemberPrice().stream().map((item) -> {
        Sms_Member_Price sms_member_price = new Sms_Member_Price();
        sms_member_price.setMember_level_name(item.getName());
        sms_member_price.setMember_price(item.getPrice());
        sms_member_price.setId(item.getId());
        sms_member_price.setSku_id(skuByReductionTo.getSku_id());
        sms_member_price.setAdd_other(1);
        return sms_member_price;
      }).collect(Collectors.toList());
      sms_member_price_service.saveBatch(sms_member_priceList);
    }

  /**
   * 后台显示所有满减
   * @param pages
   * @param sms_sku_full_reduction
   * @return
   */
  @Override
  public LayuiUtils<Sms_Sku_Full_Reduction> find_sku_full_reduction(Page<Sms_Sku_Full_Reduction> pages, Sms_Sku_Full_Reduction sms_sku_full_reduction) {
    LayuiUtils<Sms_Sku_Full_Reduction> layuis = new LayuiUtils<>();
    QueryWrapper<Sms_Sku_Full_Reduction> query = new QueryWrapper<>();
    if (sms_sku_full_reduction.getSku_id() != null){
      query.eq("sku_id",sms_sku_full_reduction.getSku_id());
    }
    Page<Sms_Sku_Full_Reduction> sms_sku_full_reductionPage = sms_sku_full_reduction_mapper.selectPage(pages,query);
    layuis.setCount(sms_sku_full_reductionPage.getTotal());
    layuis.setData(sms_sku_full_reductionPage.getRecords());
    return layuis;
  }

  /**
   * 后台新增满减
   * @param sms_sku_full_reduction
   * @return
   */
//  @Override
//  public R insert_sku_full_reduction(Sms_Sku_Full_Reduction sms_sku_full_reduction) {
//    int insert = sms_sku_full_reduction_mapper.insert(sms_sku_full_reduction);
//    if (insert > 0){
//      return R.success();
//    }
//    return R.error(500,"添加失败");
//  }

  /**
   * 后台删除满减
   * @param id
   * @return
   */
//  @Override
//  public R delete_sku_full_reduction(List<Long> id) {
//    int i = sms_sku_full_reduction_mapper.deleteBatchIds(id);
//    if (i > 0){
//      return R.success();
//    }
//    return R.error(500,"删除失败");
//  }

  /**
   * 后台修改满减
   * @param sms_sku_full_reduction
   * @return
   */
//  @Override
//  public R update_sku_full_reduction(Sms_Sku_Full_Reduction sms_sku_full_reduction) {
//    int i = sms_sku_full_reduction_mapper.updateById(sms_sku_full_reduction);
//    if (i > 0){
//      return R.success();
//    }
//    return R.error(500,"修改失败");
//  }
}
